package com.itestor.iptv.modules.common.actions;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.itestor.iptv.modules.common.domain.RegionManage;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;
import com.itestor.iptv.modules.ne.service.RobotManageService;
import com.itestor.iptv.modules.ne.vo.RobotManageOBJ;

public class IndexRobotTree extends DispatchAction{

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(IndexRobotTree.class);
    private RegionManageService regionManageService;
    private RobotManageService robotManageService;
	
	public void setRegionManageService(RegionManageService regionManageService) {
		this.regionManageService = regionManageService;
	}
	
	public void setRobotManageService(RobotManageService robotManageService) {
		this.robotManageService = robotManageService;
	}

	public ActionForward getRegionManage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<RegionManage> regionManageList = regionManageService.getRootRegionList();
		request.setAttribute("regionManageList", regionManageList);
		return mapping.findForward("success");
	}
	
	public ActionForward getOtherRegionManage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		StringBuffer temp = new StringBuffer();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		List<RegionManage> regionManageList = regionManageService.getRegionListByParentId(Integer.valueOf(id));
		List<Object[]> robotManageList = robotManageService.getAllRobotManage(id);
		temp.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		temp.append("<tree>");
		for (RegionManage regionManage : regionManageList) {
			temp.append("<tree text=\"");
			temp.append(regionManage.getRegionName());
			temp.append("\" src=\"common.indextree.do?method=getRobotManage&amp;id="+regionManage.getIdRegion()+"\" action=\"javascript:tonclick1(");
			temp.append("'" + regionManage.getIdRegion() + "','region'");
			temp.append(")\"/>");
		}
		for (Object[] robotManage : robotManageList) {
			temp.append("<tree text=\"");
			temp.append(robotManage[1]);
			temp.append("\" src=\"\" action=\"javascript:tonclick1(");
			temp.append("'" + robotManage[0] + "','robotManage'");
			temp.append(")\"/>");
		}
		temp.append("</tree>");
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/xml; charset=UTF-8");
		out.println(temp.toString());
		out.flush();
		out.close();
		return null;
	}
	
	public ActionForward getRobotManage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		StringBuffer temp = new StringBuffer();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		List<Object[]> robotManageList = robotManageService.getAllRobotManage(id);
		temp.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		temp.append("<tree>");
		for (Object[] robotManage : robotManageList) {
			temp.append("<tree text=\"");
			temp.append(robotManage[1]);
			temp.append("\" src=\"\" action=\"javascript:tonclick1(");
			temp.append("'" + robotManage[0] + "','robotManage'");
			temp.append(")\"/>");
		}
		temp.append("</tree>");
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/xml; charset=UTF-8");
		out.println(temp.toString());
		out.flush();
		out.close();
		return null;
	}
}
